<?php
/**
 * 视图控制器
 */
namespace app\manage\controller\orderlogistics;

use app\manage\controller\Base;
class Api extends Base
{

    /**物流货单列表**/
    public function index(){
        $params = $this->request->param();

        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $data = model('OrderLogistics')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);
    }

    /**
     * 导出物流货单
     * @return [type] [description]
     */
    public function export_index(){
        $params = $this->request->param();
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $query = new \app\common\model\OrderLogistics;
        $datas = $query->getPageDatas($where,['logis_no','logisname','create_time','logis_order_id']);
        $excelData = "物流货单号\t物流公司\t物流创建时间\t物流订单号\t\n";
        foreach($datas as $value):
            $excelData .= $value['logis_no']."\t";
            $excelData .= $value['logisname']."\t";
            $excelData .= $value['create_time']."\t";
            $excelData .= $value['logis_order_id']."\t";
            $excelData .= "\n";
        endforeach;
        $excelData = iconv('utf-8','gb2312',$excelData);
        exportExcel(time().'.xls',$excelData);
    }

    public function parseWhere($where){
        $array = [];
        foreach($where as $key => $value):
            if(!$value)
                continue;
            switch ($key) {
                case 'logis_id':
                    $array['logis_id'] = $value;
                    break;
                case 'logis_no':
                    $array['logis_no'] = $value;
                    break;
                case 'start':
                    $array['create_time'] = ['>=',strtotime($value)];
                    break;
                case 'end':
                    $array['create_time'] = ['<=',strtotime($value)];
                    break;
                default:
                    $array[$key] = $value;
                    break;
            }
        endforeach;
        return $array;
    }
}